package com.myh.controller;

import com.myh.pojo.User;
import com.myh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author 莫耀华
 * @date 2020-08-23 16:35
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 去登录
     * @return
     */
    @GetMapping(value = {"/index","/","/index.html"})
    public String goLogin(){
        return "login";
    }

    /**
     * 登录方法
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(User user, HttpSession session){
        User userDb = userService.login(user);
        if(userDb!=null){
            //登录成功
            session.setAttribute("user",userDb);
            return "redirect:/file/showAll";
        }else {
            //失败
            return "redirect:/user/index";
        }
    }

    @GetMapping(value = {"/logout"})
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/user/index";
    }
}
